weather_df = 
  rnoaa::meteo_pull_monitors(
    c("USW00094728", "USW00022534", "USS0023B17S"),
    var = c("PRCP", "TMIN", "TMAX"), 
    date_min = "2021-01-01",
    date_max = "2022-12-31") |>
  mutate(
    name = case_match(
      id, 
      "USW00094728" ~ "CentralPark_NY", 
      "USW00022534" ~ "Molokai_HI",
      "USS0023B17S" ~ "Waterhole_WA"),
    tmin = tmin / 10,
    tmax = tmax / 10) |>
  select(name, id, everything())
## using cached file: /Users/zhangshizhe/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USW00094728.dly
## date created (size, mb): 2024-09-26 10:23:14.171516 (8.651)
## file min/max dates: 1869-01-01 / 2024-09-30
## using cached file: /Users/zhangshizhe/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USW00022534.dly
## date created (size, mb): 2024-09-26 10:23:25.989412 (3.932)
## file min/max dates: 1949-10-01 / 2024-09-30
## using cached file: /Users/zhangshizhe/Library/Caches/org.R-project.R/R/rnoaa/noaa_ghcnd/USS0023B17S.dly
## date created (size, mb): 2024-09-26 10:23:29.76067 (1.036)
## file min/max dates: 1999-09-01 / 2024-09-30

make first plot.

ggplot(weather_df, aes(x=tmin, y=tmax))+
  geom_point()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggp_weather = 
  weather_df |>
  ggplot(aes(x = tmin, y = tmax)) 

ggp_weather + geom_point()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

Fancier scatterplot

weather_df |> 
  ggplot(aes(x=tmin, y=tmax, color = name))+
  geom_point(alpha = 0.3, size = 0.8) +
  geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

Where you definine geom_point

weather_df |> 
  ggplot(aes(x=tmin, y=tmax))+
  geom_point(aes(color=name), alpha = 0.3, size = 0.8) +
  geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

using faceting real quick

weather_df |> 
  ggplot(aes(x=tmin, y=tmax, color=name))+
  geom_point(alpha = 0.3) +
  geom_smooth(se=FALSE)+
  facet_grid(. ~name)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

more interesting scatter plot

weather_df |> 
  ggplot(aes(x=date, y=tmax, color=name, size=prcp))+
  geom_point(alpha = 0.3)+
  geom_smooth(se=FALSE)+
  facet_grid(.~name)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: The following aesthetics were dropped during statistical transformation: size.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?
## The following aesthetics were dropped during statistical transformation: size.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?
## The following aesthetics were dropped during statistical transformation: size.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?
## Warning: Removed 19 rows containing missing values or values outside the scale range
## (`geom_point()`).

weather_df |> 
  filter(name == "CentralPark_NY") |> 
  mutate(
    tmax_fahr = tmax *(9/5)+32,
    tmin_fahr = tmin *(9/5)+32,
  ) |> 
  ggplot(aes(x=tmin_fahr, y=tmax_fahr))+
  geom_point()+
  geom_smooth(method="lm", se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

small things

weather_df |> 
  ggplot(aes(x=tmin, y=tmax))+
  geom_point(aes(color=name), alpha = 0.3)+
  geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

weather_df |> 
  ggplot(aes(x=tmin, y=tmax))+
  geom_hex()
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_binhex()`).

weather_df |> 
  ggplot(aes(x=tmin, y=tmax))+
  geom_point(color="blue")
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).

Univariate plots

weather_df |> 
  ggplot(aes(x=tmin, fill = name))+
  geom_histogram(position = "dodge")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_bin()`).

how to fix

weather_df |> 
  ggplot(aes(x=tmin, fill = name))+
  geom_histogram()+
  facet_grid(.~name)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_bin()`).

density plot?

weather_df |> 
  ggplot(aes(x=tmin, fill = name))+
  geom_density(alpha=.3)
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_density()`).

weather_df |> 
  ggplot(aes(x=name, y=tmin, fill=name))+
  geom_boxplot()
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

violin plot

weather_df |> 
  ggplot(aes(x=name, y=tmin, fill = name))+
  geom_violin(alpha=.3)+
  stat_summary(fun = "median", color = "blue")
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_ydensity()`).
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_summary()`).
## Warning: Removed 3 rows containing missing values or values outside the scale range
## (`geom_segment()`).

ridge plot

ggplot(weather_df, aes(x = tmax, y = name)) + 
  geom_density_ridges(scale = .85)
## Picking joint bandwidth of 1.54
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_density_ridges()`).

weather_df |> 
  ggplot(aes(x = prcp, fill = name)) + 
  geom_density(alpha=.3)
## Warning: Removed 15 rows containing non-finite outside the scale range
## (`stat_density()`).

Saving and embedding plots

ggp_weather = 
  ggplot(weather_df, aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5) 

ggsave("ggp_weather.pdf", ggp_weather, width = 8, height = 5)
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
ggp_weather = 
  ggplot(weather_df, aes(x = tmin, y = tmax)) + 
  geom_point(aes(color = name), alpha = .5)